package com.sunzm.common.utils.mysql;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 构建SQL语句工具类
 *
 * @author Administrator
 * @version 1.0
 * @date 2021-06-20 1:21
 */
public class SQLBuildUtilsDemo {
    public static void main(String[] args) {
        String tableName = "rpt_icall_cdr_task_report";

        //主键对应的Key-Value
        HashMap<String, String> primaryKeyMap = new HashMap<>(3);
        primaryKeyMap.put("company_id", "cmp001");
        primaryKeyMap.put("task_id", "tsk002");
        primaryKeyMap.put("result_date", "2021-02-14");

        //需要更新的字段对应的Key-Value
        HashMap<String, String> updateFieldMap = new HashMap<>(1);
        updateFieldMap.put("task_name", "任务001");

        //需要累加的字段对应的Key-Value
        HashMap<String, String> accumulateFieldMap = new HashMap<>(2);
        accumulateFieldMap.put("dailed_custom_count", "1");
        accumulateFieldMap.put("dailed_count", "0");

        //需要求最大值的字段对应的Key-Value
        HashMap<String, String> maxFieldMap = new HashMap<>(2);
        maxFieldMap.put("max_duration", "31000");
        maxFieldMap.put("max_count", "200");

        //需要求最小值的字段对应的Key-Value
        HashMap<String, String> minFieldMap = new HashMap<>(2);
        minFieldMap.put("min_duration", "15000");
        minFieldMap.put("min_count", "10");

        String[] primaryKeyArray = new String[primaryKeyMap.size()];
        String[] updateKeyrray = new String[updateFieldMap.size()];
        String[] accumulateKeyArray = new String[accumulateFieldMap.size()];
        String[] maxKeyArray = new String[maxFieldMap.size()];
        String[] minKeyArray = new String[minFieldMap.size()];

        String[] primaryValueArray = new String[primaryKeyMap.size()];
        String[] updateValuerray = new String[updateFieldMap.size()];
        String[] accumulateValueArray = new String[accumulateFieldMap.size()];
        String[] maxValueArray = new String[maxFieldMap.size()];
        String[] minValueArray = new String[minFieldMap.size()];

        int i = 0;
        Iterator<Map.Entry<String, String>> entryIterator = primaryKeyMap.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<String, String> entry = entryIterator.next();
            primaryKeyArray[i] = entry.getKey();
            primaryValueArray[i] = entry.getValue();
            i ++;
        }

        i = 0;
        entryIterator = updateFieldMap.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<String, String> entry = entryIterator.next();
            updateKeyrray[i] = entry.getKey();
            updateValuerray[i] = entry.getValue();
            i ++;
        }

        i = 0;
        entryIterator = accumulateFieldMap.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<String, String> entry = entryIterator.next();
            accumulateKeyArray[i] = entry.getKey();
            accumulateValueArray[i] = entry.getValue();
            i ++;
        }

        i = 0;
        entryIterator = maxFieldMap.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<String, String> entry = entryIterator.next();
            maxKeyArray[i] = entry.getKey();
            maxValueArray[i] = entry.getValue();
            i ++;
        }

        i = 0;
        entryIterator = minFieldMap.entrySet().iterator();
        while (entryIterator.hasNext()){
            Map.Entry<String, String> entry = entryIterator.next();
            minKeyArray[i] = entry.getKey();
            minValueArray[i] = entry.getValue();
            i ++;
        }

        //生成预编译语句测试
       /* String buidPrepareSQLStr = SQLBuildUtils.buidPrepareSQL(tableName, primaryKeyArray, updateKeyrray, accumulateKeyArray, maxKeyArray, minKeyArray);

        System.out.println(buidPrepareSQLStr);*/
    }
}
